﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace TCC.Common
{
    public static class Utils
    {
        public static class Conversores
        {
            public static int ConverteTextoParaInteiro(string texto)
            {
                int valor;

                return int.TryParse(texto, out valor) ? valor : 0;
            }
        }

        public static class Seguranca
        {
            public static string EncriptarDecriptar(string texto)
            {
                var inSb = new StringBuilder(texto);
                var outSb = new StringBuilder(texto.Length);
                char c;

                for (int i = 0; i < texto.Length; i++)
                {
                    c = inSb[i];
                    c = (char)(c ^ 129);
                    outSb.Append(c);
                }
                return outSb.ToString();

            }

            public static string Hash(string str)
            {

                var md5 = new MD5CryptoServiceProvider();
                byte[] byteArray = Encoding.ASCII.GetBytes(str);
                byteArray = md5.ComputeHash(byteArray);
                string hashedValue = "";

                foreach (byte b in byteArray)
                {
                    hashedValue += b.ToString("x2");
                }

                return hashedValue;
            }
        }
    }
}